<template>
  <div class="wrapper"
       :class="{ error }">
    <input class="zeus-input"
           :value="modelValue"
           type="text"
           :disabled="disabled"
           :readonly="readonly"
           @change="$emit('change',$event.target.value)"
           @input="input($event.target.value)"
           @focus="$emit('focus',$event.target.value)"
           @blur="$emit('blur',$event.target.value)" />
    <span v-if="error"
          class="error-text">{{ error }}</span>
  </div>
</template>

<script lang="ts">
export default {
  props: {
    modelValue: {
      type: String,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    readonly: {
      type: Boolean,
      default: false,
    },
    error: {
      type: String,
    },
  },
  setup(props, context) {
    const input = (e) => {
      context.emit("input", e);
      context.emit("update:modelValue", e);
    };
    return {
      input,
    };
  },
  emits: ["change", "input", "focus", "blur"],
};
</script>

<style lang="scss">
$height: 32px;
$border-color: #999;
$border-color-hover: #666;
$border-radius: 4px;
$font-size: 12px;
$box-shadow-color: rgba(0, 0, 0, 0.5);
$red: #f1453d;
.wrapper {
  font-size: $font-size;
  display: inline-block;

  .zeus-input {
    height: $height;
    border: 1px solid $border-color;
    border-radius: $border-radius;
    padding: 0 8px;
    font-size: inherit;
    margin: 10px;
    &:hover {
      border-color: $border-color-hover;
    }
    &:focus {
      box-shadow: inset 0 1px 3px $box-shadow-color;
      outline: none;
    }
    &[disabled],
    &[readonly] {
      border-color: #bbb;
      color: #bbb;
      cursor: no-drop;
    }
  }
  &.error {
    display: inline-flex;
    flex-direction: column;
    input {
      border-color: $red;
      &:focus {
        box-shadow: none;
        outline: none;
      }
      &:hover {
        border-color: $red;
      }
    }

    .error-text {
      color: $red;
      margin-left: 10px;
    }
  }
}
</style>
